// pages/edit/edit.js
const req = require('../../utils/requests.js')
const util = require('../../utils/util.js')
const api = require('../../utils/api.js')

Page({
  data: {
    hiddenmodalput: true, // 标题弹窗的显示的开关
    music: {},
    specialEffect: { // 特效的枚举类型
      names: ['放大', '缩小', '左移', '右移', '上移', '下移'],
      keys: ['zoomIn', 'zoomOut', 'moveLeft', 'moveRight', 'moveUp', 'moveDown']
    },
    tmpTitle: "", // 临时标题, 记录输入的值, 用来更新相册信息
    album: { // 相册数据
    }
  },
  /**
   * 编辑标题时的弹窗显示
   */
  editTitle: function (event) {
    this.setData({
      hiddenmodalput:false
    })
  },
  /**
   * 记录输入事件 
   */
  onBindInput: function (e) {
    var value = e.detail.value
    this.data.tmpTitle = value
  },
  /**
   * 标题输入时的确认点击
   */ 
  onCancel: function (e) {
    this.setData({
      hiddenmodalput: true,
    })

  },
  onConfirm: function (e) {
    this.data.album.title = this.data.tmpTitle
    this.setData({
      hiddenmodalput: true,
      album: this.data.album
    })
     
  },
  /**
   * 选择图片并上传
   */
  chooseImages: function (event) {
    var that = this

    var leftCount = 20 - this.data.album.datas.length
    if (leftCount >= 9) {
      leftCount = 9
    } else if (leftCount == 0) {
      wx.showToast({
        icon: 'none',
        title: '当前最多只能添加20张图片~',
      })
      return 
    }

    util.chooseImages({
      count: leftCount,
      success: function(res) {
        console.log(res)
        var album = that.data.album
        if (!album.datas) {
          album.datas = []
        }
        console.log(album.datas)
        res.forEach(function(item, index) {
          album.datas.push({
            url: item.url,
            type: 'image',
            width: item.width,
            height: item.height,
            se: {
              duration: 2000,
              animation: that.randomSpecialEffect(),
            }
          })
        })
        
        console.log(album.datas)
        that.setData({
          album: album
        })
      }
    })
  },
  /**
   * 删除图片
   */
  deleteImage: function (e) {
    var index = e.currentTarget.dataset.i
    console.log(index)
    var album = this.data.album
    var datas = album.datas
    datas.splice(index, 1)
    album.datas = datas
    this.setData({
      album: album
    })
  },
  /**
   * 向上移动图片
   */
  moveUpImage: function (e) {
    var index = e.currentTarget.dataset.i
    if (index == 0) {
      return
    }
    var album = this.data.album
    var datas = album.datas
    var up = datas[index - 1]
    var down = datas[index]
    datas[index] = up
    datas[index - 1] = down
    album.datas = datas
    this.setData({
      album: album
    })
  },
  /**
   * 向下移动图片
   */
  moveDownImages: function (e) {
    var index = e.currentTarget.dataset.i
    var album = this.data.album
    var datas = album.datas
    if (index == datas.length - 1) {
      return
    }
    var up = datas[index]
    var down = datas[index + 1]
    datas[index] = down
    datas[index + 1] = up
    album.datas = datas
    this.setData({
      album: album
    })
  },
  /**
   * 特效设置
   */
  specialEffect: function (e) {
    var index = e.currentTarget.dataset.i
    var that = this
    var album = this.data.album
    var specialEffect = this.data.specialEffect
    wx.showActionSheet({
      itemList: specialEffect['names'],
      success: function (res) {
        var name = specialEffect['keys'][res.tapIndex]
        var datas = album.datas
        datas[index].se.animation = name
        that.setData({
          album: album
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  /**
   * 获取随机特效值
   */
  randomSpecialEffect: function() {
    var specialEffect = this.data.specialEffect.keys
    var index = Math.floor(Math.random() * (specialEffect.length - 1))
    return specialEffect[index]
  },
  /**
   * 预览相册内容
   */
  preview: function (e) {
    getApp().globalData.album = this.data.album
    wx.navigateTo({
      url: '/pages/detail/detail?preview=1',
    })
  },
  /**
   * 同步相册内容到服务器
   */
  save: function (e) {
    var that = this
    wx.showLoading({
      title: '正在保存',
    })
    // 如果有图片, 先同步图片
    // 再更新相册信息.
    api.album_save(this.data.album, function(res) {
      console.log(res)
      req.PUT({
        action: '/wxapi/albums/',
        data: res,
        showError: true,
        complete: function (code, msg, data) {
          wx.hideLoading()
          wx.showToast({
            title: '保存好啦',
          })
          that.setData({
            album: res
          })
        }
      })
    })  
  },
  setMusic: function (e) {
    // 跳转更新背景音乐的界面
    wx.navigateTo({
      url: '/pages/music/music',
    })
  },
  updateMusic:function(music) {
    var album = this.data.album
    album.bg_music = music
    this.setData({
      album: album
    })
  },
  /**
   * 设置封面
   */
  setCover: function(e) {
    var that = this
    util.chooseImages({
      count: 1, 
      success: function (res) {
        that.data.album.cover = res[0]
        that.setData({
          album: that.data.album
        })
      }
    })
  },
  /**
   * 启动后如果参数包含id, 则加载相册内容
   */
  onLoad: function (options) {
    if (!(options && options.id)) {
      return
    }
    var albumId = options.id
    var that = this;
    req.GET({
      action: '/wxapi/albums/' + albumId + '/',
      complete: function(code, msg, data) {
        console.log(data)
        that.setData({
          album: data
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})